package com.ht.pms.core.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ht.pms.core.dao.SalaryTemplateDao;
import com.ht.pms.core.dto.tempalte.Template;
import com.ht.pms.core.dto.tempalte.TemplateDetailParam;
import com.ht.pms.core.dto.tempalte.TemplateReq;

@Repository
public class SalaryTemplateDaoImpl implements SalaryTemplateDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Template> selectTemplateList(TemplateReq req) {
		String sql = null;
		if (StringUtils.isEmpty(req.getKeyworld())) {
			sql = "select * from t_template limit ? , ?";
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Template>(Template.class),
					(req.getPage() - 1) * req.getLimit(), req.getLimit());
		} else {
			sql = "select * from t_template where template_code like ? or template_name like ?   limit ? , ? ";
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Template>(Template.class),
					"%" + req.getKeyworld() + "%", "%" + req.getKeyworld() + "%", (req.getPage() - 1) * req.getLimit(),
					req.getLimit());
		}
	}
	
	@Override
	public List<Template> selectTemplateListByType() {
		String sql = "select * from t_template where template_type in (1,2,3)";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Template>(Template.class));
	}
	
	@Override
	public int selectTemplateCount(TemplateReq req) {
		String sql = null;
		if (StringUtils.isEmpty(req.getKeyworld())) {
			sql = "select count(*) from t_template";
			return jdbcTemplate.queryForObject(sql, Integer.class);
		} else {
			sql = "select count(*) from t_template where template_code like ? or template_name like ?";
			return jdbcTemplate.queryForObject(sql, Integer.class, "%" + req.getKeyworld() + "%", "%" + req.getKeyworld() + "%");
		}
	}

	@Override
	public Template selectTemplateByTemplateCode(String templateCode) {
		String sql = "select * from t_template where template_code = ?";
		try {
			return jdbcTemplate.queryForObject(sql, new Object[] { templateCode },
					new BeanPropertyRowMapper<Template>(Template.class));
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}


	@Override
	public int insertTemplate(TemplateDetailParam req) {
			String sql = "insert into t_template (template_name, template_code, template_desc) values (?, ?, ?)";
			return jdbcTemplate.update(sql, req.getTemplateName(), req.getTemplateCode(), req.getTemplateDesc());
	}

	@Override
	public int UpdateTemplate(TemplateDetailParam req) {
		
		String sql = "update t_template set template_name = ?, template_desc = ? where template_code = ?";
		return jdbcTemplate.update(sql, req.getTemplateName(), req.getTemplateDesc(), req.getTemplateCode());
		
	}



	

}
